package org.azmat.student.ResultVO;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

@Schema(description = "返回结果")
@Data
@NoArgsConstructor
public class Result<T> {
    // 这里约定数字0表示成功，-1表示失败
    @Schema(description = "返回码")
    private String code;
    // 返回描述信息
    @Schema(description = "返回信息")
    private String msg;
    // 返回数据
    @Schema(description = "返回数据")
    private T data;
    public Result(T data) {
        this.data = data;
    }
    // 执行成功，但没有数据，适用于增，删，改
    @Schema(description = "执行成功，但没有数据")
    public static Result success(String msg) {
        Result result = new Result();
        result.setCode("0");
        result.setMsg(msg);
        return result;
    }

    // 执行成功，有数据，适用于查询
    @Schema(description = "执行成功，有数据")
    public static <T> Result<T> success(T data,String msg) {
        Result<T> result = new Result<>(data);
        result.setCode("0");
        result.setMsg(msg);
        return result;
    }


    // 执行失败
    @Schema(description = "执行失败")
    public static Result error(String msg) {
        Result result = new Result();
        result.setCode("-1");
        result.setMsg("error");
        return result;
    }
}
